package com.yiwei.ydd.fragment;

import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.text.Spanned;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.yiwei.ydd.R;
import com.yiwei.ydd.api.Api;
import com.yiwei.ydd.util.Network;
import com.yiwei.ydd.util.RxLifeUtil;
import com.yiwei.ydd.util.Spans;
import com.yiwei.ydd.util.ToastUtil;
import com.yiwei.ydd.view.NumAnim;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by xsooy-pc on 18-10-10.
 */

public class ShowDataFragment extends BaseFragment {
    @BindView(R.id.txt_one)
    TextView txtOne;
    @BindView(R.id.txt_two)
    TextView txtTwo;
    @BindView(R.id.txt_three)
    TextView txtThree;

    Unbinder unbinder;
    @BindView(R.id.refresh)
    SwipeRefreshLayout refresh;

    @Override
    protected int setLayoutId() {
        return R.layout.fragment_show_data;
    }

    @Override
    protected void initView() {

        refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getIndexDatas();
            }
        });
    }



    public void getIndexDatas() {
//        "可使用", "已使用", "已过期"
        //状态（0：过期，1:有效，2：已使用)
        if (!Network.checkNetwork(getContext())) {
            ToastUtil.show(getContext(), "网络连接失败");
            return;
        }
        refresh.setRefreshing(true);

        Api.api_service.getIndexDatas().compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        refresh.setRefreshing(false);
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    NumAnim.startAnim(txtOne, result.datas.money, 1000);     //设置滚动动画持续1秒
                    NumAnim.startAnim(txtTwo, result.datas.gif, 1000);     //设置滚动动画持续1秒
                    NumAnim.startAnim(txtThree, result.datas.user, 1000);     //设置滚动动画持续1秒
                });
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
